const SERVICE = require("../../lib/js/service.js");
const Golbal = require("../../lib/js/global.js");

Page({
  data:{
    inputValue:null,
    isViewPwd:false,
    pwdFocus:false
  },
  onLoad(options){
    let RegInfo = JSON.parse(options.RegInfo);
    this.setData({
      mobile: RegInfo.mobile,
      code: RegInfo.code
    });
  },
  
  /*分享*/ 
  onShareAppMessage(){
  },
  
  /*手机号*/
  Mobile(e){
    this.setData({
      inputVal:e.detail.value,
      isActive: Golbal.checkPwd(e.detail.value) === false ? false : true
    });
    
  },

  /*清除手机号*/ 
  Clear(){
    this.setData({
      inputVal: null,
      isActive: false,
      pwdFocus:true
    });
  },

  /*查看密码*/ 
  viewPwd(){
    let td = this.data;
    this.setData({
      isViewPwd: !td.isViewPwd,
      pwdFocus:true
    });    
  },

  /**
   * @explain formSubmit 注册
   * @param phone  手机号
   * @param code  验证码
   * @param password  密码
   * 
   * */ 
  formSubmit(e){
    let _this = this, td = this.data, pwd = e.detail.value.pwd;
    if (Golbal.checkPwd(pwd) !== false){
      console.log("注册");
      wx.showLoading({
        title: "注册中...",
        icon: "none"
      });
      SERVICE.POST(SERVICE.URL.reg,{
        phone: td.mobile,
        code: td.code,
        password: pwd,
        register:"register",
      },(status,res)=>{
        wx.hideLoading();
        if (status){
          wx.showToast({
            title: res.data.message,
            icon: "none",
            mask: true,
            success(){
              _this.login({
                phone: td.mobile,
                code: td.code,
                password: pwd,
              });
            }
          });
        }else{
          wx.showToast({
            title: res.data.message,
            icon: "none",
            mask: true
          });
        }
      });
    }
  },

  login(params){
    SERVICE.POST(SERVICE.URL.login,params, (status, res) => {
      if (status) {
        wx.setStorageSync("thirdCookie", res.data.thirdCookie);
        wx.showToast({
          title: res.data.message,
          icon: "none",
          mask: true,
          success() {
            setTimeout(() => {
              wx.reLaunch({
                url: "../index/index",
              });
            }, 1500);
          }
        });

      } else {
        wx.showToast({
          title: res.data.message,
          icon: "none",
          mask: true
        });
      }
    });
  }

});